package com.dyna.digit.flowable.controller;

import cn.hutool.core.lang.Assert;
import com.dyna.digit.flowable.domain.DefinitionsInfo;
import com.dyna.digit.flowable.domain.DeploymentEntity;
import com.dyna.digit.flowable.domain.DigitTask;
import com.dyna.digit.flowable.domain.param.TaskSubmitParam;
import com.dyna.digit.flowable.domain.request.TaskReq;
import com.dyna.digit.flowable.domain.vo.FlowHistoryVo;
import com.dyna.digit.flowable.service.DigitFlowableService;
import com.dyna.domain.resp.PageResult;
import com.dyna.domain.resp.R;
import com.dyna.domain.resp.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * @author dyna
 */
@Api(value = "数字化项目管理系统/工作流程")
@RestController
@AllArgsConstructor
@RequestMapping(value = "/proj")
public class DigitFlowableController {

    private DigitFlowableService flowableService;

    @ApiOperation(value = "部署流程")
    @GetMapping("/flow/deploy")
    public R<Object> deploy(@RequestParam(value = "bpmnFile") String bpmnFile, @RequestParam(value = "flowName",required = false) String flowName) {
        Assert.notBlank(bpmnFile,ResultCode.PARAM_MISS.getMessage());
        return R.data(flowableService.deploy(bpmnFile,flowName));
    }

    @ApiOperation(value = "查询部署流程")
    @GetMapping("/flow/queryDeploys")
    public R<PageResult<DeploymentEntity>> queryDeploys(@RequestParam(value = "name",required = false) String name) {
        return R.data(flowableService.queryDeploys(name));
    }

    @ApiOperation(value = "获取流程定义信息")
    @GetMapping("/flow/definition")
    public R<DefinitionsInfo> definition(@RequestParam(value = "deployId",required = false) String deployId) {
        return R.data(flowableService.definition(deployId));
    }

    @ApiOperation(value = "启动流程")
    @PostMapping("/flow/startDefine")
    public R<String> startDefine(@RequestBody @Validated TaskReq taskReq) {
        Assert.notBlank(ResultCode.PARAM_MISS.getMessage());
        return R.data(flowableService.startDefinition(taskReq));
    }

    @ApiOperation(value = "查询任务")
    @GetMapping("/flow/queryTask")
    public R<PageResult<DigitTask>> queryTask(@RequestParam(value = "key") String key,
                                              @RequestParam(value = "assignee",required = false) String assignee,
                                              @RequestParam(value = "projectId",required = false) Integer projectId,
                                              @RequestParam(value = "projectName",required = false) String projectName
    ) {
        return R.data(flowableService.queryTask(key,assignee,projectId,projectName));
    }

    @ApiOperation(value = "完成任务")
    @PostMapping("/flow/compileTask")
    public R<Object> compileTask(@RequestParam(value = "taskId") String taskId,
                            @RequestBody @Validated TaskSubmitParam taskSubmitParam) {
        flowableService.compileTask(taskId,taskSubmitParam);
        return R.success(ResultCode.SUCCESS);
    }

    @ApiOperation(value = "查询历史任务")
    @GetMapping("/flow/queryHistoryTask")
    public R<PageResult<FlowHistoryVo>> queryHistoryTask(@RequestParam(value = "key") String id) {
        return R.data(flowableService.queryHistoryTask(id));
    }

    @ApiOperation(value = "删除任务")
    @DeleteMapping("/flow/delTask")
    public R<Object> delTask(@RequestParam(value = "id") String processInstanceId, @RequestParam(value = "reason") String reason) {
        flowableService.delTask(processInstanceId,reason);
        return R.success(ResultCode.SUCCESS);
    }

    @ApiOperation(value = "删除历史任务")
    @DeleteMapping("/flow/delHistoryTask")
    public R<Object> delHistoryTask(@RequestParam(value = "processInstanceId") String processInstanceId) {
        flowableService.delHistoryTask(processInstanceId);
        return R.success(ResultCode.SUCCESS);
    }

}
